<?php 
	
require_once("ui.php");
require_once("report.template.php");
require_once("report.categories.php");

class ReportFlag {
	public $column;
	public $description;
	public $tabId;
	public $value;
	
	function __construct($flagColumn, $flagDescription, $tabId) {
		$this->column = $flagColumn;
		$this->description = $flagDescription;
		$this->tabId = $tabId;
		$this->value = false;
	}
	
	public function getFlagId() {
		return $this->tabId . "-" . $this->column;
	}
}

class ReportTab {
	public $categories;
	public $tabId;
	public $flagsTable;
	
	function __construct($tabId, $flagsTable, $categories) {
		$this->tabId = $tabId;
		$this->categories = $categories;
		$this->flagsTable = $flagsTable;
	}

	private function getFirstCategory() {
		reset($this->categories);
		return key($this->categories);		
	}

	public function getRadioGroupName() {
		return "radioGroup" . $this->tabId;
	}
	
	private function getFlagColumns() {
		global $db; 	
				
		$sql = "SELECT column_name AS Name,
						column_comment AS Description
				FROM information_schema.columns
				WHERE table_name = '$this->flagsTable'
				AND column_name LIKE 'Flag%'";
	
		$db->sql_query($sql);
		$sqlItems = $db->sql_fetchall();
		
		$flags = array();
		
		foreach ($sqlItems as $i => $sqlItem) {
			$name = $sqlItem['Name'];
			$description = $sqlItem['Description'];
			
			$flag = new ReportFlag($name, $description, $this->tabId);
			array_push($flags, $flag);
		}
		
		return $flags;
	}
	
	public function printFlagsDiv() {
		$flags = $this->getFlagColumns();

		if (count($flags) == 0) {
			return;
		}
		
		echo 'Flags:<br>';
		
		foreach ($flags as $i => $flag) {
			$checkboxId = $flag->getFlagId();
			
			printPostCheckbox($checkboxId, $flag->description);	
		}
	}
	
	public function getPostedFlags() {
		$flags = $this->getFlagColumns();

		foreach ($flags as $i => $flag) {
			$post = readPost($flag->getFlagId());
			
			$flag->value = ($post == "on");
		}
		
		return $flags;
	}
	
	public function printTabContent() {
		echo '<div class="reportTab" id="' . $this->tabId. '">';

		echo '<div id="detectionRightDiv">';
		$this->printFlagsDiv();
		echo '</div>';

		echo '<div id="detectionLeftDiv">';
		
		if (count($this->categories) > 1) {
			echo 'Category:';
			$this->printTabSelect();
			echo '<br>';
		}
		
		$subcategoriesSelectLabel = (count($this->categories) > 1) ? "Subcategory:" : "Category:";
		echo $subcategoriesSelectLabel;
				
		foreach ($this->categories as $i => $category) {
			$category->printCategoryContent();
		}

		echo '</div>';
		echo '</div>';
	}
	
	public function getSelectId() {
		return 'select' . $this->tabId;
	}
	
	public function printTabSelect() {
		$optionsInfo = array();

		foreach ($this->categories as $i => $category) {
			$optionInfo['value'] = $category->categoryId;
			$optionInfo['description'] = $category->categoryName;
			
			array_push($optionsInfo, $optionInfo);
		}
		
		printSelect($this->getSelectId(), 
			'selectCategory',
			'{tabId : "' . $this->tabId . '"}',
			$optionsInfo,
			null,
			"reportCategorySelect"
		);
	}
}

?>
